使用 Open API - REST Countries 作為 HTTP Request 範例
使用 Compose UI 的 LazyVerticalGrid
或 LazyColumn
呈現資料 List,取代 RecyclerView
@Composable
@Preview(showBackground = true)
fun CountryList(list: List<CountryData>){
LazyVerticalGrid(
columns = GridCells.Fixed(1),
contentPadding = PaddingValues(
start = 12.dp,
top = 16.dp,
end = 12.dp,
bottom = 16.dp
),
content = {
items(list.size) { index ->
Card(
modifier = Modifier
.padding(4.dp)
.fillMaxWidth()
) {
Text(
text = list[index].name,
fontWeight = FontWeight.Bold,
fontSize = 30.sp,
color = Color(0xFFFFFFFF),
textAlign = TextAlign.Center,
modifier = Modifier.padding(16.dp)
)
}
}
}
)
}
依據 Response 欄位,改寫 Data class 內容:
@JsonClass(generateAdapter = true)
data class CountryData(
val name: NameData,
val tld: List<String>,
val cca2: String,
val ccn3: String,
val cca3: String,
val independent: Boolean,
val status: String,
val unMember: Boolean,
val currencies: Map<String, CurrencyData>,
val idd: IddData,
val capital: List<String>,
val altSpellings: List<String>,
val region: String,
val subregion: String,
val languages: Map<String, String>
)
data class NameData(
val common: String,
val official: String,
val nativeName: Map<String, NativeNameData>
)
data class NativeNameData(
val official: String,
val common: String
)
data class CurrencyData(
val name: String,
val symbol: String
)
data class IddData(
val root: String,
val suffixes: List<String>
)